package com.consorzioimpresa.resuite.domain

import grails.plugins.springsecurity.Secured;

@Secured(['ROLE_ADMIN','ROLE_CDIMPRESA'])
class ImmobileController {
	
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

//    def search = {
//        redirect(action: "list", params: params)
//    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [immobileInstanceList: Immobile.list(params), immobileInstanceTotal: Immobile.count()]
    }

    def create = {
        def immobileInstance = new Immobile()
        immobileInstance.properties = params
        return [immobileInstance: immobileInstance]
    }

    def save = {
        def immobileInstance = new Immobile(params)
        if (immobileInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'immobile.label', default: 'Immobile'), immobileInstance.id])}"
            redirect(action: "show", id: immobileInstance.id)
        }
        else {
            render(view: "create", model: [immobileInstance: immobileInstance])
        }
    }

    def show = {
        def immobileInstance = Immobile.get(params.id)
		
        if (!immobileInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'immobile.label', default: 'Immobile'), params.id])}"
            redirect(action: "list")
        }
        else {
            [immobileInstance: immobileInstance]
        }
    }

    def edit = {
        def immobileInstance = Immobile.get(params.id)
        if (!immobileInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'immobile.label', default: 'Immobile'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [immobileInstance: immobileInstance]
        }
    }

    def update = {
        def immobileInstance = Immobile.get(params.id)
        if (immobileInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (immobileInstance.version > version) {
                    
                    immobileInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'immobile.label', default: 'Immobile')] as Object[], "Another user has updated this Immobile while you were editing")
                    render(view: "edit", model: [immobileInstance: immobileInstance])
                    return
                }
            }
            immobileInstance.properties = params
            if (!immobileInstance.hasErrors() && immobileInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'immobile.label', default: 'Immobile'), immobileInstance.id])}"
                redirect(action: "show", id: immobileInstance.id)
            }
            else {
                render(view: "edit", model: [immobileInstance: immobileInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'immobile.label', default: 'Immobile'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def immobileInstance = Immobile.get(params.id)
        if (immobileInstance) {
            try {
                immobileInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'immobile.label', default: 'Immobile'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'immobile.label', default: 'Immobile'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'immobile.label', default: 'Immobile'), params.id])}"
            redirect(action: "list")
        }
    }
	
	def search ={
		if(params.q && !params.q.isEmpty())
			render(view: "search", model: [searchResult: Immobile.search(params.q,params),params:params])
			else{
				params.q="null"
				render(view: "search", model: [params:params])
			}
			
	}
}
